package com.zq.behavior.mediator;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public interface ChatRoom<U extends User> {
    void register(U user);

    void send(String fromId, String String, String content);
}

class ConcreteChatRoom implements ChatRoom<Person> {

    Map<String, Person> map = new HashMap<>();

    @Override
    public void register(Person user) {
        Objects.requireNonNull(user);
        map.putIfAbsent(user.getUserId(), user);
    }

    @Override
    public void send(String fromId, String toId, String content) {
        Person u1 = Objects.requireNonNull(map.get(fromId));
        Person u2 = Objects.requireNonNull(map.get(toId));

        u2.receive(fromId, content);
    }
}
